//封装本地存储存储数据与读取数据方法

interface Tag {
  title: string
  name: string
  close: boolean
}

let homeTag: Tag = {
  title: '首页',
  name: 'home',
  close: false,
}
//存储数据
export const SET_TAGs = (tag: Tag[]) => {
  const tagString = JSON.stringify(tag)
  localStorage.setItem('TAGS', tagString)
}
//本地存储获取数据
export const GET_TAG = () => {
  const tagString = localStorage.getItem('TAGS')

  // 判断 tagString 是否为 null 或空数组
  if (tagString === null || tagString === '[]') {
    return [homeTag] as Tag[]
  }
  return JSON.parse(tagString) as Tag[]
}

//存储数据
export const SET_THIS_TAG = (name: string) => {
  localStorage.setItem('THIS_TAGS', name)
}
//本地存储获取数据
export const GET_THIS_TAG = () => {
  return localStorage.getItem('THIS_TAGS') || 'home'
}

//清除数据
export const CLEAR_TAG = () => {
  localStorage.removeItem('TAGS')
  localStorage.removeItem('THIS_TAGS')
}
